package ms.unicorn.ui.figures;

import ms.unicorn.ui.edit.parts.IEntityEditPart;
import ms.unicorn.util.FigureConstant;

import org.eclipse.draw2d.ConnectionAnchor;
import org.eclipse.draw2d.IFigure;
import org.eclipse.draw2d.geometry.Dimension;
import org.eclipse.draw2d.geometry.Point;
import org.eclipse.gmf.runtime.draw2d.ui.figures.IAnchorableFigure;
import org.eclipse.gmf.runtime.gef.ui.figures.DefaultSizeNodeFigure;

public class EntityPlateFigure extends DefaultSizeNodeFigure {

	private IEntityEditPart editpart;
	private IAnchorableFigure fig; 

	public EntityPlateFigure(Dimension defSize, IEntityEditPart editpart) {
		super(defSize);
		this.editpart = editpart;
	}
	
	public IEntityEditPart getEntityEditPart() {
		return editpart;
	}
	
	@Override
	public ConnectionAnchor getConnectionAnchor(String terminal) {
		if (getAnchorFigure() != null)
			return getAnchorFigure().getConnectionAnchor(terminal);
		return super.getConnectionAnchor(terminal);
	}
	
	@Override
	public String getConnectionAnchorTerminal(ConnectionAnchor c) {
		if (getAnchorFigure() != null)
			return getAnchorFigure().getConnectionAnchorTerminal(c);
		return super.getConnectionAnchorTerminal(c);
	}
	
	@Override
	public ConnectionAnchor getSourceConnectionAnchorAt(Point p) {
		if (getAnchorFigure() != null)
			return getAnchorFigure().getSourceConnectionAnchorAt(p);
		return super.getSourceConnectionAnchorAt(p);
	}
	
	@Override
	public ConnectionAnchor getTargetConnectionAnchorAt(Point p) {
		if (getAnchorFigure() != null)
			return getAnchorFigure().getTargetConnectionAnchorAt(p);
		return super.getTargetConnectionAnchorAt(p);
	}
	
	protected IAnchorableFigure getAnchorFigure() {
		if (fig == null) {
			 IFigure temp = getEntityEditPart().getFigure(FigureConstant.FIGURE_ANCHORS);
			 fig = temp instanceof IAnchorableFigure ? (IAnchorableFigure)temp : this;
		}
		return fig != this ? fig : null;
	}

}
